import { FormInstance } from 'antd';
export class ObservableForm {
  form: FormInstance;
  observerList: { [key: string]: any } = {};
  // historyList: { [key: string]: any } = {};

  constructor(private formInstance: FormInstance) {
    this.form = formInstance;
  }

  subscribe(name: string, fn: (value: any, values: any) => void) {
    if (!this.observerList[name]) {
      this.observerList[name] = [fn];
    } else {
      this.observerList[name].push(fn);
    }
    // if (this.historyList[name]) {
    //   fn(this.historyList[name]);
    // }
  }

  publish(name: string, value: any, values: any) {
    if (this.observerList[name]) {
      // this.historyList[name] = value;
      this.observerList[name].forEach((fn: any) => {
        fn(value, values);
      });
    } else {
      // this.historyList[name] = value;
    }
  }

  unsubscribe(name: string, fn: any) {
    const index = this.observerList[name].findIndex((item: any) => item === fn);
    this.observerList[name].splice(index, 1);
  }
}
